// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: GHZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//

private alias object.value0 : object.timer
private alias object.value1 : Text.Line

private alias object.value2 : object.drawX
private alias object.value3 : object.drawY
private alias object.value4 : object.letterID
private alias object.value5 : object.textamount
private alias object.value6 : Selected.Tape	//0 = Slenderman, 1 = Freddys
private alias object.value7 : Tape.Ypos

private alias object.value8 : TV.Slider	
private alias object.value9 : TV.Ypos	

private alias object.value10 : Object.SlenderDA
private alias object.value11 : Object.FreddyDA

private alias object.value12 : Object.TouchTimer

public alias saveRAM[6905] : saved.SlenderOST
public alias saveRAM[295]: saved.mobile
public alias saveRAM[286] : saved.language

reserve function SoundTest_SlenderTracks
reserve function TextDrawing
reserve function MobilingIt

//States
private alias 0	:	SOUNDTEST_ACTIVATE_0
private alias 1	:	SOUNDTEST_FADEIN_1
private alias 2	:	SOUNDTEST_CHOOSETAPE_2
private alias 3	:	SOUNDTEST_FADEOUTTAPE_3
private alias 4	:	SOUNDTEST_TVRISE_4
private alias 5	:	SOUNDTEST_MAIN_5
private alias 6	:	SOUNDTEST_LEAVE_6
private alias 7	:	SOUNDTEST_FADEINTAPES_7


private alias 0	:	SLENDER_SELECTED_0
private alias 1	:	FREDDY_SELECTED_1


function MobilingIt
	CheckTouchRect(0, 0, screen.xsize, screen.ysize)
	if checkResult == -1
		Object.TouchTimer = false
	end if
	if Object.TouchTimer == 0
		switch object.state
			case SOUNDTEST_CHOOSETAPE_2
			case SOUNDTEST_MAIN_5
				temp0 = 0
				temp1 = 80
				CheckTouchRect(temp0, 0, temp1, 82)
				if CheckResult > -1
					Object.TouchTimer = 1
					inputPress.buttonB = 1
				end if

				temp0 = screen.xsize
				temp0 -= 75
				CheckTouchRect(temp0, 0, screen.xsize, 82)
				if CheckResult > -1
					Object.TouchTimer = 1
					inputPress.start = 1
				end if

				temp0 = object.value17
				temp0 += screen.xcenter
				temp0 -= 200
				temp1 = object.value17
				temp1 += screen.xcenter
				temp1 -= 10
				CheckTouchRect(temp0, 96, temp1, 200)
				if CheckResult > -1
					Object.TouchTimer = 1
					inputpress.left = 1
				end if	

				temp0 += 210
				temp1 += 200
				CheckTouchRect(temp0, 96, temp1, 200)
				if CheckResult > -1
					Object.TouchTimer = 1
					inputpress.right = 1
				end if				
			break
		end switch
	end if
end function



function SoundTest_SlenderTracks
	StopMusic()
	if  Selected.Tape == SLENDER_SELECTED_0
		switch Object.SlenderDA
		case 0
			SetMusicTrack("SlenderMenu.ogg", 0, 1)
		break
		case 1
			if saved.SlenderOST == 0 //me
				SetMusicTrack("BossIntro.ogg", 0, 1)
			else
				SetMusicTrack("BossIntroM.ogg", 0, 1)
			end if
		break
		case 2
			if saved.SlenderOST == 0 //me
				SetMusicTrack("ForeverTrouble.ogg", 0, 376200)
			else
				SetMusicTrack("ForeverTroubleM.ogg", 0, 376200)
			end if
		break
		case 3
			SetMusicTrack("ForeverLose.ogg", 0, 0)
		break	
		case 4
			SetMusicTrack("victory.ogg", 0, 0)
		break
		case 5
			if saved.SlenderOST == 0
				SetMusicTrack("SlenderBattle.ogg", 0, 1)
			else
				SetMusicTrack("SlendyBattleM.ogg", 0, 1)
			end if
		break
		case 6
			SetMusicTrack("DeltaDie.ogg", 0, 1)
		break
		case 7
			SetMusicTrack("ThisIsA.ogg", 0, 1)
		break
		case 8
			if saved.SlenderOST == 0
				SetMusicTrack("FreddyBuster.ogg", 0, 1)
			else
				SetMusicTrack("SlendyBattle2M.ogg", 0, 1)
			end if
		break
		end switch
	else

		switch Object.FreddyDA
		case 0
			SetMusicTrack("BearMenu.ogg", 0, 1)
		break
		case 1
			SetMusicTrack("OfficeVibes.ogg", 0, 1)
		break
		case 2
			SetMusicTrack("OfficeLose.ogg", 0, 0)
		break
		case 3
			SetMusicTrack("BearWin.ogg", 0, 0)
		break	
		case 4
			SetMusicTrack("OfficeTense.ogg", 0, 1)
		break
		case 5
			SetMusicTrack("bearwin2.ogg", 0, 0)
		break
		end switch
	end if

	PlayMusic(0)
end function




function TextDrawing
	temp0 = object.drawX
	temp1 = object.textamount
	while temp1 > 0
	// Get the actual character at the current char pos
	GetTextInfo(object.letterID, 1, TEXTINFO_TEXTDATA, Text.Line, ArrayPos0)

	Object.Frame = 0
	
	// Take uppercase letters and turn them into a matching Sprite Frame
	if object.letterID > 64
		if object.letterID < 91
			Object.Frame  = object.letterID
			Object.Frame -= 64
		end if
	end if
	
	// Take lowercase letters and turn them into a matching Sprite Frame
	if object.letterID > 96
		if object.letterID < 123
			Object.Frame  = object.letterID
			Object.Frame -= 96
		end if
	end if

		//ASCII Table required!
		switch object.letterID
			case 33	//!
				DrawSpriteScreenFX(37, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break		
			case 63	//?
				DrawSpriteScreenFX(38, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break					
			case 42
				// Notably, the space character (32) is skipped over here, as it shouldn't draw anything
				

				DrawSpriteScreenFX(27, FX_INK, object.drawX, object.drawY)
				

				// Move to the next letter
				ArrayPos0++
				
				// And decrease the remaining letter count, as well
				temp1--
				
				// Each letter should be spaced 8 pixels apart
				object.drawX += 0x8
				break
			case 64	//@	LINE SKIPPER
				object.drawY += 16
				object.drawX = temp0
				

				// Move to the next letter
				ArrayPos0++
				
				// And decrease the remaining letter count, as well
				temp1--
			break
			default
			// Notably, the space character (32) is skipped over here, as it shouldn't draw anything
			

			DrawSpriteScreenFX(Object.Frame, FX_INK, object.drawX, object.drawY)

			// Move to the next letter
			ArrayPos0++
			
			// And decrease the remaining letter count, as well
			temp1--
			
			// Each letter should be spaced 8 pixels apart
			object.drawX += 0x8
			break
		end switch
	loop
end function

//if being stupid was a job

event ObjectMain
	Callfunction(MobilingIt)
	switch object.state
	case SOUNDTEST_ACTIVATE_0
		object.timer = 255
		object.alpha = 255
		object.state++
		SetClassicFade(0, 0, 0, 255)	
	break
	case SOUNDTEST_FADEIN_1	//fade in
		Selected.Tape = -1
		if object.timer > -1
			object.timer -= 16
		else
			object.timer = 0	
			object.state++
		end if
		SetClassicFade(0, 0, 0, Object.Timer)	
	break	
	case SOUNDTEST_CHOOSETAPE_2
		Text.Line = 0
		GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
		if object.textamount == temp0
		else
			object.textamount++
			PlaySfx(SfxName[Text], false)
		end if

		if inputPress.right == 1
			PlaySfx(SfxName[Score Add], false)		
			Selected.Tape = FREDDY_SELECTED_1
		end if

		if inputPress.left == 1
			PlaySfx(SfxName[Score Add], false)		
			Selected.Tape = SLENDER_SELECTED_0
		end if	

		if inputpress.buttonA == 1
			inputpress.start = 1
		end if

		if selected.tape > -1
			if inputpress.start == 1
				PlaySfx(SfxName[Lamp Post], false)		
				object.textamount = 0
				object.timer = 0
				object.state++
			end if
		end if

		if inputpress.buttonB == 1
			object.state = 99
		end if
	break
	case SOUNDTEST_FADEOUTTAPE_3
		object.alpha -=8
		Tape.Ypos -= 4
		if object.alpha <= 0
			object.state++
		end if
	break
	case SOUNDTEST_TVRISE_4
		if object.alpha < 255
			object.alpha += 8
		else
			object.alpha = 255
			object.state++
		end if
	break
	case SOUNDTEST_MAIN_5

		if inputpress.buttonA == 1
			inputpress.start = 1
		end if

		if selected.tape == 0
			temp1 = 1
			temp1 += Object.SlenderDA
			Text.Line = temp1
			GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
			if object.textamount == temp0

			else
				object.textamount++
				PlaySfx(SfxName[Text], false)
			end if

			if inputpress.right == 1
				object.textamount = 0
				if Object.SlenderDA > 7
					Object.SlenderDA = 0
				else
					Object.SlenderDA++
				end if
			end if

			if inputpress.left == 1
				object.textamount = 0
				if Object.SlenderDA < 1
					Object.SlenderDA = 8
				else
					Object.SlenderDA--
				end if
			end if

			if inputpress.start == 1
				Callfunction(SoundTest_SlenderTracks)
			end if
		else
			temp1 = 10
			temp1 += Object.FreddyDA
			Text.Line = temp1
			GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
			if object.textamount == temp0

			else
				object.textamount++
				PlaySfx(SfxName[Text], false)
			end if

			if inputpress.right == 1
				object.textamount = 0
				if Object.FreddyDA > 4
					Object.FreddyDA = 0
				else
					Object.FreddyDA++
				end if
			end if

			if inputpress.left == 1
				object.textamount = 0
				if Object.FreddyDA < 1
					Object.FreddyDA = 5
				else
					Object.FreddyDA--
				end if
			end if

			if inputpress.start == 1
				Callfunction(SoundTest_SlenderTracks)
			end if
		end if


		if inputpress.buttonB == 1
			Text.Line = -1
			object.textamount = 0
			StopMusic()
			Tape.Ypos = 0
			object.state++
		end if
	break
	case SOUNDTEST_LEAVE_6
		object.alpha -= 16
		if object.alpha <= 0
			object.alpha = 0
			object.state = SOUNDTEST_FADEINTAPES_7
		end if
	break
	case SOUNDTEST_FADEINTAPES_7
		object.alpha += 16
		if object.alpha >= 255
			object.state = SOUNDTEST_CHOOSETAPE_2
		end if
	break
	case 99
		object.timer += 16
		if object.timer >= 256
			stage.activeList = BONUS_STAGE
			stage.listPos = StageName[B - SLENDERMENU]
			ReturnToSlenderMenu = 1
			LoadStage()	
		end if
		SetClassicFade(0, 0, 0, Object.Timer)	
	break
	end switch
end event

event ObjectDraw
	DrawRect(0, 0, screen.xsize, screen.ysize, color1, color1, color1, 255)

	temp2 = 0
	while temp2 < screen.xsize
		DrawSpriteScreenXY(46, temp2, screen.ycenter)
		temp2+= 48
	loop

	DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, 96)	


	object.inkEffect = INK_ALPHA


	switch object.state
	case SOUNDTEST_ACTIVATE_0
	case SOUNDTEST_FADEIN_1
	case SOUNDTEST_CHOOSETAPE_2
	case SOUNDTEST_FADEOUTTAPE_3
	case SOUNDTEST_FADEINTAPES_7
	case 99

		Tape.Ypos++
		temp4 = 0
		Sin256(temp4, Tape.Ypos)
		temp4 /= 40
		temp4 += screen.ycenter
		//tapes
		temp0 = screen.xcenter
		temp0 += 8
		if Selected.Tape == FREDDY_SELECTED_1
			DrawSpriteScreenFX(48, FX_INK, temp0, temp4)	///slender
		else
			DrawSpriteScreenFX(48, FX_INK, temp0, screen.ycenter)	///slender
		end if
		temp0 -= 128
		if Selected.Tape == SLENDER_SELECTED_0
			DrawSpriteScreenFX(47, FX_INK, temp0, temp4)	///slender
		else
			DrawSpriteScreenFX(47, FX_INK, temp0, screen.ycenter)	///slender
		end if

		//arrows
		temp0 = screen.xcenter
		temp0 += 128
		DrawSpriteScreenFX(49,FX_INK,  temp0, screen.ycenter)	///slender
		temp0 -= 288
		DrawSpriteScreenFX(50,FX_INK,  temp0, screen.ycenter)	///slender

		if saved.mobile == 1
			DrawSpriteScreenFX(58, FX_INK, 8, 12)
			temp0 = screen.xsize
			temp0 -= 64
			DrawSpriteScreenFX(59, FX_INK, temp0, 12)
		end if

	break
	case SOUNDTEST_TVRISE_4

		temp4 = -775
		//Scrolling
		TV.Slider = temp4
		TV.Slider -= screen.ycenter
		TV.Slider /= 8
		TV.Slider += 24
		temp1 = TV.Slider
		temp1 -= Tape.Ypos
		temp2 = temp1
		Abs(temp2)

		//Make sure it's exactly in the spot
		if temp2 < 6
			if Tape.Ypos > TV.Slider
				Tape.Ypos--
			else
				if Tape.Ypos < TV.Slider
					Tape.Ypos++
				else
					Tape.Ypos = TV.Slider
				end if
			end if
		else
			temp1 >>= 2
			Tape.Ypos += temp1
		end if

		temp4 = Tape.Ypos
		temp4 += screen.ycenter
		TV.Ypos = temp4
		TV.Ypos -= 2

		temp0 = screen.xcenter
		DrawSpriteScreenFX(51,FX_INK,  temp0, TV.Ypos)	///slender

		//arrows
		temp0 = screen.xcenter
		temp0 += 128
		DrawSpriteScreenFX(49,FX_INK,  temp0, screen.ycenter)	///slender
		temp0 -= 288
		DrawSpriteScreenFX(50,FX_INK,  temp0, screen.ycenter)	///slender



	break
	case SOUNDTEST_MAIN_5
	case SOUNDTEST_LEAVE_6

		if saved.mobile == 1
			DrawSpriteScreenFX(58, FX_INK, 8, 12)
			temp0 = screen.xsize
			temp0 -= 64
			DrawSpriteScreenFX(59, FX_INK, temp0, 12)
		end if

		//arrows
		temp0 = screen.xcenter
		temp0 += 128
		DrawSpriteScreenFX(49,FX_INK,  temp0, screen.ycenter)	///slender
		temp0 -= 288
		DrawSpriteScreenFX(50,FX_INK,  temp0, screen.ycenter)	///slender



		temp0 = screen.xcenter
		DrawSpriteScreenFX(51,FX_INK, temp0, TV.Ypos)	///slender
		if selected.tape == 0
			switch Object.SlenderDA
			case 4
				DrawSpriteScreenFX(53,FX_INK, temp0, TV.Ypos)	///slender
			break
			case 5
				DrawSpriteScreenFX(54,FX_INK, temp0, TV.Ypos)	///slender
			break		
			case 8
				DrawSpriteScreenFX(55,FX_INK, temp0, TV.Ypos)	///slender
			break					
			default
				DrawSpriteScreenFX(52,FX_INK, temp0, TV.Ypos)	///slender
			break
			end switch
		else
			switch Object.FreddyDA
			case 1
			case 2
			case 4
				DrawSpriteScreenFX(57,FX_INK, temp0, TV.Ypos)	///slender
			break					
			default
				DrawSpriteScreenFX(56,FX_INK, temp0, TV.Ypos)	///slender
			break
			end switch
		end if

		temp1 = TV.Ypos
		temp1 += 18
		temp0 -= 94
		DrawRect(temp0, temp1, 188, 156, 0, 0, color1, 64)	
	break
	end switch

	ArrayPos0 = 0	//starting letter

	switch object.state
	case SOUNDTEST_MAIN_5
		object.drawY = 64
		temp3 = screen.xcenter
		temp3 -= 62	
	break
	default
		object.drawY = 35
		temp3 = screen.xcenter
		temp3 -= 86		
	break
	end switch
	

	object.drawX = temp3

	CallFunction(TextDrawing)

	if object.state == 0
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, 255)	
	end if

	
end event

event ObjectStartup
	LoadSpriteSheet("3D_Test/SoundTest.gif")
	switch saved.language
		case 0
			LoadTextFile(1, "Data/Game/SoundTest.txt")
		break
		case 1
			LoadTextFile(1, "Data/Game/SoundTest.txt")
		break
		case 2
			LoadTextFile(1, "Data/Game/Spanish/SoundTest.txt")
		break
		case 3
			LoadTextFile(1, "Data/Game/French/SoundTestFR.txt")
		break					
	end switch
	
	SpriteFrame(0, 0, 0, 0, 0, 0)	//0 the bar

	// #1 -> #26 - Alphabet
	temp1 = 1
	while temp1 < 51
		temp0 = 37
		while temp0 < 135
			SpriteFrame(0, 0, 8, 16, temp0, temp1)
			temp0 += 9
		loop
		temp1 += 17
	loop

	//numbers	//34
	temp1 = 1
	while temp1 < 51
		temp0 = 1
		while temp0 < 37
			SpriteFrame(0, 0, 8, 16, temp0, temp1)
			temp0 += 9
		loop
		temp1 += 17
	loop

	SpriteFrame(0, 32, 48, 48, 136, 1)//46	checker bg

	SpriteFrame(0, -16, 112, 64, 185, 1)//47 tape
	SpriteFrame(0, -16, 112, 64, 299, 1)//48 tape


	//arows
	SpriteFrame(0, 0, 32, 32, 412, 1)//49 one
	SpriteFrame(0, 0, 32, 32, 445, 1)//50 two

	//turn this tv off
	SpriteFrame(-112, 0, 224, 208, 287, 303)//51 TV	

	//sketchs
	SpriteFrame(-94, 18, 188, 156, 1, 355)//52 	
	SpriteFrame(-94, 18, 188, 156, 1, 198)//53	
	SpriteFrame(-94, 18, 188, 156, 323, 146)//54
	SpriteFrame(-94, 18, 188, 156, 1, 512)//55	

	SpriteFrame(-94, 18, 188, 156, 1, 669)//56	
	SpriteFrame(-94, 18, 188, 156, 190, 669)//57

	SpriteFrame(-3, -6, 64, 64, 222, 382) //58	
	SpriteFrame(-3, -6, 64, 64, 222, 447) //59			

	// Place the HUD object into reserved object slot 4 and initialise its values
	Object[0].Type = TypeName[Sound Test]
	
	// The object should always be run, regardless if it's within camera bounds or not
	Object[0].Priority = PRIORITY_ACTIVE
	
	// Make the HUD draw ontop of all other sprites, too
	Object[0].DrawOrder = 6

	

end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("IIZ/Objects1.gif")
	SpriteFrame(0, 0, 64, 64, 153, 154)
end event
